// pages/reward/my.js
import Router from '../../utils/router.js';
import base from '../../config/base.js';
import util from '../../utils/util.js'
import rewardsj from '../../services/rewardsj.js'
import shareInfoJson from '../../utils/hbshare.js'
import storeservices from '../../services/store.js'
import redpacket from '../../utils/redpacket.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: '',
    activity: '',
    noreward: base.static + '/redenvelopes/noreward.png',
    shenmiren: base.static + '/redenvelopes/shenmiren.png', 
    list1length: -1,
    list1: [],
    list2length: -1,
    list2: [],
    style: 1,
    page: 1,
    offset: 20,
    //加载状态  1 还有数据可加载，0已全部加载完成  -1加载失败
    loadflag: 1,
    //加载 开关 true可加载
    listflag: false,
    now_bounty: "0.00",  //提现赏金
    all_bounty: "0.00",  //累计赏金
    all_bonus: "0.00",    //在路上的赏金
    //分享 控制 
    share: {
      isShow: false,
      animate: false
    },
    transmit: {//formid 
      style: 'position:absolute;top:0;left:0;width:100%;height:100%;background:red;opacity:0;padding:0;',
      value: ''
    },
    user_info: {},
    activity: {}
  },
  goGonglue() {
    shareInfoJson.cardInfo.goGonglue();
  },
  openshare: function (e) {
    if (this.data.cheap_status) {
      shareInfoJson.getbonusNum(this);
    } else {
      wx.showToast({
        title: '活动已结束！',
        icon: 'none'
      })
    }
  },
  goputforward() { 
    let now_bounty = this.data.now_bounty;
    if (now_bounty && parseFloat(now_bounty) < 20){
      wx.showToast({
        title: '满20元才可以提现哦~',
        icon: 'none',
        duration: 2500
      })
      return;
    }
    Router.go('putforward', `now_bounty=${now_bounty}`);
  },
  tpreward(e) {
    let style = this.data.style,
      d = e.currentTarget.dataset;
    if(this.data.style!=d.style){
      this.setData({
        style: d.style,
        loadflag: 1,
        ['list' + style]: [],
        ['list' + style + 'length']: -1,
        page: 1
      });
      this.getdetails();
    }
  },
  getreward(){
    let now_bounty = 0.00,
      all_bounty = 0.00,
      all_bonus = 0.00;
    rewardsj.getBounty({}, res =>{
      if(res && res.code==1){
        let data = res.data;
        if (data.now_bounty != null && data.now_bounty != '' && !isNaN(Number(data.now_bounty))) {
          data.now_bounty = util.toDecimal2(util.floatAccDiv(data.now_bounty, 1));
        }
        if (data.all_bounty != null && data.all_bounty != '' && !isNaN(Number(data.all_bounty))) {
          data.all_bounty = util.toDecimal2(util.floatAccDiv(data.all_bounty, 1));
        }
        if (data.all_bonus != null && data.all_bonus != '' && !isNaN(Number(data.all_bonus))) {
          data.all_bonus = util.toDecimal2(util.floatAccDiv(data.all_bonus, 1));
        }
        this.setData({
          now_bounty: data.now_bounty,
          all_bounty: data.all_bounty,
          all_bonus: data.all_bonus,
        });
      }
    }, res=>{
    });
  },
  getdetails(){
    let style = this.data.style,
      fn = '',
      loadflag = this.data.loadflag;
    if (!loadflag){
      return ;
    }
    if(style == 1){
      fn = 'bountygetBountyBillList'
    }else if(style == 2){
      fn = 'bountygetBonusList'
    }
    let data = this.data,
      list = [],
      listlength = 0;
    rewardsj[fn]({
      page: data.page,
      offset: data.offset
    }, res => {
      if(res && res.code==1){
        list = res.data;
        for (let index in list) {
          if (list[index].bill_money != null && list[index].bill_money != '' && !isNaN(Number(list[index].bill_money))) {
            list[index].bill_money = util.toDecimal2(util.floatAccDiv(list[index].bill_money, 1));
          }
          if (list[index].bybslt_money != null && list[index].bybslt_money != '' && !isNaN(Number(list[index].bybslt_money))) {
            list[index].bybslt_money = util.toDecimal2(util.floatAccDiv(list[index].bybslt_money, 1));
          }
        }
        list = this.data['list' + style].concat(list);
        let page = this.data.page;
        if(list.length>=res.count){
          loadflag = 0;
        } else {
          page++;
        }
        this.setData({
          ['list' + style]: list,
          ['list' + style + 'length']: list.length,
          loadflag, 
          page
        });
      } else {
        list = this.data['list' + style];
        loadflag = 0;
        this.setData({
          ['list'+style]: list,
          ['list' + style + 'length']: list.length,
          loadflag
        });
      }
    }, res => {
      this.setData({
        listflag: true
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdetails();
    wx.hideShareMenu();
    //获取 红包 id
    shareInfoJson.getSendRedEnvelope(this);
    redpacket.getcheap_goods(this);
    let user_info = wx.getStorageSync('user_info');
    let activity = wx.getStorageSync('activity');
    this.setData({
      user_info: user_info,
      activity: activity  
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getreward();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this,
      listflag = that.data.listflag,
      loadflag = that.data.loadflag;
    if (!listflag && loadflag) {
      return;
    }
    that.setData({
      listflag: false
    });
    that.partnerfn();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let shareObj = {
      title: shareInfoJson.cardInfo.titlefn(),
      path: shareInfoJson.cardInfo.pathFn(this) + `&hb_sharePath=myreward`,
      imageUrl: shareInfoJson.cardInfo.imageUrl,
      success: function (res) {
        console.log(res, 'success');
      },
      fail: function (res) {
        console.log(res, 'fail');
      },
      complete: function () {
        console.log('complete');
      }
    }
    // if(options.from == 'button'){
    //   shareObj.path = '/pages/my/joinshopman?shareType=1';
    // }
    return shareObj;
  }
})